<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/6/7
 * Time: 14:38
 * 原始视频替换程序
 */
require_once dirname(__DIR__)."/common.php";
use app\cls\tom\Lock;
use app\cls\tom\FFmpeg;
use app\lib\db\DbException;
use app\cls\tom\SysResource;
use app\cls\tom\Process;
use app\cls\tom\Tools;
use app\model\Video;

$process = new Process(__FILE__);
$process->setProcessNum(5);
$process->initProcess();
$process->isTryError = false;
$redis = SysResource::getRedis();
$db =  SysResource::getDB(true);
$ffmpeg = new FFmpeg();
/*===============
 *初始化模型
 ================*/
$videoModel = new Video($db);
$cosUploadConfig = \app\cls\tom\Config::load("cos_upload");
$lock = new Lock("replace_material");
    try{
        $readyList = $videoModel->get_replace_material(20);
//        var_dump($readyList);exit;
        //如果不为空就执行程序
        if(!empty($readyList)){
            foreach($readyList as &$row){
                $lockStatus = $lock->tryLock($row["video_id"]);
                $lockStatus = true;
                if($lockStatus){
                    //1.新上传的视频已经转码的话，就去修改精品视频表和用户素材表
                    if(!empty($row["transcode_video_url"]) && !empty($row["transcode_video_no_music_url"]) && !empty($row["cut_music_url"])){
                        //2.去精品视频表查询和本视频uuid相同的视频
                        $quality_video=$videoModel->video_update($row["video_uuid"]);
                        if(!empty($quality_video)){
//                        var_dump(1);exit;
                            //精品视频表数据
                            $data1['quality_video_url']=$row["transcode_video_url"];
                            $data1['quatily_pic']=$row["video_small_pic"];
                            $data1['quality_join_type']=$row["video_join_type"];
                            $data1['quality_join_type_sub']=$row["video_join_sub"];
                            $data1['quality_desc']=$row["video_desc"];
                            $data1['duration']=$row["video_time"];
                            $data1['width1']=$row["width"];
                            $data1['height1']=$row["height"];
                            $data1['first_pic2']=$row["first_pic1"];
                            //用户素材表数据
                            $data2['video_url']=$row["transcode_video_url"];
                            $data2['no_music_video_url']=$row["transcode_video_no_music_url"];
                            $data2['music']=$row["cut_music_url"];
                            $data2['duration']=$row["video_time"];
                            $data2['join_type']=$row["video_join_type"];
                            $data2['join_type_sub']=$row["video_join_sub"];
                            $data2['pic']=$row["video_small_pic"];
                            $data2['video_desc']=$row["video_desc"];
                            $data2['width2']=$row["width"];
                            $data2['height2']=$row["height"];
                            $data2['first_pic3']=$row["first_pic1"];
                            foreach($quality_video as $k=>$v){
                                //修改精品视频表
                                $db->update("quality_video",$data1,["id"=>$v["id"]]);
                                //修改用户素材表
                                $db->update("user_material",$data2,["id"=>$v["user_material_id"]]);
                            }
                            $db->update("video",["is_update"=>1],["video_id"=>$row["video_id"]]);
                        }else{
                            $db->update("video",["is_update"=>1],["video_id"=>$row["video_id"]]);
//                        continue;
                        }
                    }
                    $lock->unLock($row["video_id"]);
                }

            }
        }

    }catch (DBException $e){
        throw $e;
        echo $e->getMessage();
        sleep(2);
        $process->restartProcess();
    }catch (RedisException $e){
        sleep(2);
        $process->restartProcess();
    }catch (Exception $e){
        if(!empty($row)){
            $db->update("video",["is_update"=>2],["video_id"=>$row["video_id"]]);
            $lock->unLock($row["video_id"]);
        }
        Tools::writeLog(["msg"=>"原始视频替换程序","detail"=>["msg"=>$e->getMessage(),"video_id"=>$id],"mix"]);
        sleep(2);
        $process->restartProcess();
    }

